package com.dwxt.race.service;

import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.StatusCode;
import com.dwxt.common.util.DateUtils;
import com.dwxt.race.dao.RaceMapper;
import com.dwxt.race.entity.Race;
import com.dwxt.race.entity.RaceExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 赛事信息管理
 */
@Service
public class RaceService {

    @Autowired
    private RaceMapper raceMapper;

    /**
     * 赛事查询
     *
     * @param race
     * @return
     */
    public BaseResult createRace(Race race) {
        if (race.getRegistrationStartTime().before(race.getRegistrationEndTime()) && race.getRaceStartTime().before(race.getRaceEndTime()) && race.getRaceEndTime().after(race.getRegistrationEndTime())) {
            int i = raceMapper.insertSelective(race);
            if (i > 0) {
                return BaseResult.ok();
            } else {
                return BaseResult.build(StatusCode.FAIL);
            }
        } else {
            return BaseResult.build(StatusCode.RACETIME_CANNOT_TOO_EARLY);
        }
    }

    /**
     * 更新赛事
     *
     * @param race
     * @return
     */
    public BaseResult updateRace(Race race) {
        if (race.getRegistrationStartTime().before(race.getRegistrationEndTime()) && race.getRaceStartTime().before(race.getRaceEndTime()) && race.getRaceEndTime().after(race.getRegistrationEndTime())) {
            int i = raceMapper.updateByPrimaryKey(race);
            if (i > 0) {
                return BaseResult.ok();
            } else {
                return BaseResult.build(StatusCode.FAIL);
            }
        } else {
            return BaseResult.build(StatusCode.RACETIME_CANNOT_TOO_EARLY);
        }
    }

    /**
     * 删除赛事
     *
     * @param id
     * @return
     */
    public BaseResult deleteRace(Integer id) {
//        int i = raceMapper.deleteByPrimaryKey(id);
        Race race = raceMapper.selectByPrimaryKey(id);
        if (race.getRaceStatus()==1){
            //禁用
            race.setRaceStatus(2);
        }else if (race.getRaceStatus()==2){
            //启用
            race.setRaceStatus(1);
        }
        int i = raceMapper.updateByPrimaryKeySelective(race);
        if (i > 0) {
            return BaseResult.ok();
        } else {
            return BaseResult.build(StatusCode.FAIL);
        }
    }

    /**
     * 分页查询赛事列表
     *
     * @param pageNum
     * @param pageSize
     * @param status   1.预告；2.报名中；3.进行中；4.已结束
     * @return
     */
    public BaseResult selectRaceList(Integer pageNum, Integer pageSize, Integer status) {
        //分页对象
        PageHelper.startPage(pageNum, pageSize);
        //当前系统时间
        Date date = new Date();
        RaceExample raceExample = new RaceExample();
        RaceExample.Criteria criteria = raceExample.createCriteria();
        if (status != null) {
            if (status == 1) {
                criteria.andRegistrationStartTimeGreaterThan(date);
            } else if (status == 2) {
                criteria.andRegistrationEndTimeGreaterThan(date).andRegistrationStartTimeLessThan(date);
            } else if (status == 3) {
                criteria.andRaceStartTimeLessThan(date).andRaceEndTimeGreaterThan(date);
            } else if (status == 4) {
                criteria.andRaceEndTimeLessThan(date);
            }
        }
        List<Race> races = raceMapper.selectByExample(raceExample);
        PageInfo<Race> racePageInfo = new PageInfo<>(races);
        return BaseResult.ok(racePageInfo);
    }

    /**
     * app前端查询赛事列表
     */
    public BaseResult selectRaceListFromApp(Integer pageNum, Integer pageSize, Integer status) {
        //分页对象
        PageHelper.startPage(pageNum, pageSize);
        //当前系统时间
        Date date = new Date();
        RaceExample raceExample = new RaceExample();
        RaceExample.Criteria criteria = raceExample.createCriteria();
        //启用赛事筛选
        criteria.andRaceStatusEqualTo(1);
        if (status != null) {
            if (status == 1) {
                criteria.andRegistrationStartTimeGreaterThan(date);
            } else if (status == 2) {
                criteria.andRegistrationEndTimeGreaterThan(date).andRegistrationStartTimeLessThan(date);
            } else if (status == 3) {
                criteria.andRaceStartTimeLessThan(date).andRaceEndTimeGreaterThan(date);
            } else if (status == 4) {
                criteria.andRaceEndTimeLessThan(date);
            }
        }
        List<Race> races = raceMapper.selectByExample(raceExample);
        PageInfo<Race> racePageInfo = new PageInfo<>(races);
        return BaseResult.ok(racePageInfo);
    }

    /**
     * 分页条件查询
     *
     * @param pageNum
     * @param pageSize
     * @param raceName
     * @param status
     * @return
     */
    public BaseResult selectRaceListByQuery(Integer pageNum, Integer pageSize, String raceName, Integer status) {
        //分页对象
        PageHelper.startPage(pageNum, pageSize);
        //当前系统时间
        Date date = new Date();
        RaceExample raceExample = new RaceExample();
        RaceExample.Criteria criteria = raceExample.createCriteria();
        if (StringUtils.isNotEmpty(raceName)) {
            criteria.andRaceNameLike(raceName);
        }
        if (status!=null){
            if (status == 1) {
                criteria.andRegistrationStartTimeGreaterThan(date);
            } else if (status == 2) {
                criteria.andRegistrationEndTimeGreaterThan(date).andRegistrationStartTimeLessThan(date);
            } else if (status == 3) {
                criteria.andRaceStartTimeLessThan(date).andRaceEndTimeGreaterThan(date);
            } else if (status == 4) {
                criteria.andRaceEndTimeLessThan(date);
            }
        }
        List<Race> races = raceMapper.selectByExample(raceExample);
            PageInfo<Race> racePageInfo = new PageInfo<>(races);
            return BaseResult.ok(racePageInfo);
    }

    public BaseResult selectById(Integer raceId) {
        return BaseResult.ok(raceMapper.selectByPrimaryKey(raceId));
    }

    public BaseResult selectRaceStatus(Integer raceId) {
        Date date = new Date();
        RaceExample raceExample = new RaceExample();
        //报名时间内
        raceExample.createCriteria().andIdEqualTo(raceId).andRegistrationEndTimeGreaterThan(date).andRegistrationStartTimeLessThan(date);
        List<Race> races = raceMapper.selectByExample(raceExample);
        if (races.size()>0){
            return BaseResult.ok();
        }else {
            return BaseResult.build(StatusCode.ISNOT_RACETIME);
        }
    }
}
